package com.base.app.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

public class SpCommon {
    Context context;
    SharedPreferences sp;
    SharedPreferences.Editor editor;

    /** File **/
    String fileName = "SP_COMMON";

    /**
     * 屏保
     */
    public final static String SCREENSAVER_SWITCH_ENABLE = "SCREENSAVER_SWITCH_ENABLE";//屏保是否可用
    public final static String SCREENSAVER_TIME = "SCREENSAVER_TIME";//屏保等待时长

    /**
     * 天气
     * @param context
     */
    public final static String WEATHER_CURRENT_INFO = "WEATHER_CURRENT_INFO";//当前天气

    public static SpCommon instance;

    @SuppressLint("WrongConstant")
    public SpCommon(Context context){
        this.context = context;
        sp = context.getSharedPreferences(fileName, Context.MODE_APPEND);
        editor = sp.edit();
    }

    public static SpCommon getInstance(Context context){
        if(instance==null){
            instance = new SpCommon(context);
        }
        return instance;
    }

    public void putInt(String key, int value){
        editor.putInt(key, value);
        editor.commit();
    }

    public void putString(String key, String value){
        editor.putString(key, value);
        editor.commit();
    }

    public void putBoolean(String key, boolean value){
        editor.putBoolean(key, value);
        editor.commit();
    }

    public void putFloat(String key, float value){
        editor.putFloat(key, value);
        editor.commit();
    }

    public int getInt(String key){
        return sp.getInt(key, 0);
    }
    public int getInt(String key, int defaultValue){
        return sp.getInt(key, defaultValue);
    }

    public String getString(String key){
        return sp.getString(key, "");
    }
    public String getString(String key, String defaultValue){
        return sp.getString(key, defaultValue);
    }

    public boolean getBoolean(String key){
        return sp.getBoolean(key, false);
    }
    public boolean getBoolean(String key, boolean defaultValue){
        return sp.getBoolean(key, defaultValue);
    }

    public float getFloat(String key){
        return sp.getFloat(key, 0);
    }
    public float getFloat(String key, float defaultValue){
        return sp.getFloat(key, defaultValue);
    }

    public void remove(String key){
        editor.remove(key);
        editor.apply();
    }

    public void clear(){
        editor.clear();
        editor.apply();
    }
}
